package com.htsk.city.services.wechat.aspect;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.htsk.city.services.core.domain.WxUser;
import com.htsk.city.services.wechat.annotation.GetUserInfo;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 有@GetUserInfo注解且参数为LoginUser的controller方法会进入到解析器中
 * 通过解析器查询到当前用户，并返回给controller *
 */
@Service
public class LoginUserHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {


    /**
     *
     * @param parameter
     * @return
     */
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        return parameter.hasParameterAnnotation(GetUserInfo.class);
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
        WxUser wxUser= JSONUtil.toBean((String) StpUtil.getLoginId(),WxUser.class);
        return wxUser;
    }
}
